package com.ymt.bpm.web.rest.engine.bpd;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.service.engine.bpd.BpdService;
import com.ymt.bpm.service.engine.bpd.BpdVersionCacheService;
import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Johnny on 2017/4/20.
 */
@RestController
@RequestMapping("/api/v1/engine")
public class BpdVersionResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BpdService bs;

    @Autowired
    private BpdVersionCacheService bvcs;

    @RequestMapping(value="/bpdWithVersion/{bpdId}/{versionid}", method= RequestMethod.GET)
    public JSONObject bpdWithVersion(HttpServletRequest req,
                          @PathVariable("bpdId")String bpdId,
                          @PathVariable("versionid")String versionid,
                          @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("bpdWithVersion tenantId=" + tenantId);
            log.debug("bpdWithVersion bpdId=" + bpdId);
            log.debug("bpdWithVersion versionid=" + versionid);
            log.debug("bpdWithVersion lang=" + lang);
            log.debug("bpdWithVersion timezoneOffset=" + timezoneOffset);
        }
        JSONObject bpd = bvcs.getBpdSimple(tenantId, bpdId, lang, timezoneOffset);
        JSONObject version;
        if ("current".equalsIgnoreCase(versionid)) {
            int vid = bs.getCurrentVersionId(tenantId, bpdId);
            version = bvcs.getVersionJson(tenantId, bpdId, vid);
        } else {
            version = bvcs.getVersionJson(tenantId, bpdId, Integer.parseInt(versionid));
        }
        JSONObject rtn = new JSONObject();
        rtn.put("bpd", bpd);
        rtn.put("version", version);
        return rtn;
    }

    @RequestMapping(value="/bpdWithVersionNoBpmn/{bpdId}/{versionid}", method= RequestMethod.GET)
    public JSONObject bpdWithVersionNoBpmn(HttpServletRequest req,
                                     @PathVariable("bpdId")String bpdId,
                                     @PathVariable("versionid")String versionid,
                                     @RequestParam("timezoneOffset")int timezoneOffset) {
        JSONObject rtn = bpdWithVersion(req, bpdId, versionid, timezoneOffset);
        rtn.getJSONObject("bpd").remove("bpmn");
        rtn.getJSONObject("version").remove("bpmn");
        return rtn;
    }

    @RequestMapping(value="/bpdVersion/{bpdId}/{versionid}", method= RequestMethod.GET)
    public JSONObject bpdVersion(HttpServletRequest req,
                                     @PathVariable("bpdId")String bpdId,
                                     @PathVariable("versionid")String versionid,
                                     @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("bpdVersion tenantId=" + tenantId);
            log.debug("bpdVersion bpdId=" + bpdId);
            log.debug("bpdVersion versionid=" + versionid);
            log.debug("bpdVersion lang=" + lang);
            log.debug("bpdVersion timezoneOffset=" + timezoneOffset);
        }
        JSONObject version;
        if ("current".equalsIgnoreCase(versionid)) {
            int vid = bs.getCurrentVersionId(tenantId, bpdId);
            version = bvcs.getVersionJson(tenantId, bpdId, vid);
        } else {
            version = bvcs.getVersionJson(tenantId, bpdId, Integer.parseInt(versionid));
        }
        return version;
    }

}
